import asyncio
import time

import requests

"""
    同步: sync  time.sleep 阻塞程序的执行, 效率低
    异步: async 防止耗时任务(网络请求, 数据IO操作)阻挡后续任务的执行
"""

# # 同步请求 10个url案例
# urls = [f'https://httpbin.org/get?age={i}' for i in range(10)]
# start = time.time()
# for url in urls:
#     response = requests.get(url)
#     print(response.text)
# end = time.time() - start
# print(end)


# # 异步 基础结构
# async def main():
# """
#     异步函数中要有耗时任务 await(等待耗时操作结束)
#     返回协程
#     使用asyncio.run() 启动协程
# """
#     print(1)
#     await asyncio.sleep(1)
#     print(2)
#
# asyncio.run(main())
# print("end")

# # 多个异步任务 串行
# start = time.time()
# async def task1():
#     print('start1')
#     await asyncio.sleep(3)
#     print('end1')
#
# async def task2():
#     print('start2')
#     await asyncio.sleep(5)
#     print('end2')
#
# async def main():
#     await asyncio.create_task(task1())
#     await asyncio.create_task(task2())
# asyncio.run(main())
# end = time.time() - start
# print(end)

# # 多个异步任务 并行
# start = time.time()
# async def task1():
#     print('start1')
#     await asyncio.sleep(3)
#     print('end1')
#
# async def task2():
#     print('start2')
#     await asyncio.sleep(5)
#     print('end2')
#
# async def main():
#     await asyncio.gather(task1(), task2())
#
# asyncio.run(main())
# end = time.time() - start
# print(end)

# # 异步 相似进程
# start = time.time()
# async def task(name):
#     await asyncio.sleep(3)
#     print(f'进程{name}开始')
#
# async def main():
#     tasks = [task(i) for i in range(10)]
#     await asyncio.gather(*tasks)
#
# asyncio.run(main())
# end = time.time() - start
# print(end)

import aiohttp